package com.share.config.email.template;

import com.alibaba.fastjson.JSON;
import lombok.Getter;

import java.util.Map;

@Getter
public abstract class AbstractTempleate {
    //主题
    protected String _subject;
    //接收的用户列表
    protected String _users;
    //发送人
    protected String _mailFrom = "syq";

    public abstract String getTemplate();

    /**
     * 字段替换
     *
     * @return
     */
    public String build() {
        String tmp = this.getTemplate();
        Map<String, Object> modelMap = JSON.parseObject(JSON.toJSONString(this), Map.class);
        for (Map.Entry<String, Object> entry : modelMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value == null) {
                value = "";
            }
            tmp = tmp.replaceAll("\\{" + key + "}", value.toString());
        }
        return tmp;
    }
}
